import Vue from "vue";
import Router from "vue-router";
// import Home from "../views/Home.vue"
// import Reg from "../views/Reg.vue"
// const Reg = ()=> import ("../views/Reg.vue")

/* import GoodsDetail from "../views/GoodsDetail.vue"
import My from "../views/My.vue" */
const GoodsDetail = ()=> import (/* webpackChunkName:'demot' */ "../views/GoodsDetail.vue")
const My = ()=> import (/* webpackChunkName:'demot' */ "../views/My.vue")
import Cart from "../views/Cart.vue"
import Sort from "../views/Sort.vue"
import Login from "../views/Login.vue"
import Search from "../views/Search.vue"
import Setting from "../views/Setting.vue"
import SearchResult from  "../views/SearchResult.vue"
import payMent from "../views/payMent.vue"


// 把插件安装到Vue里
Vue.use(Router);

let router =  new Router({
    mode:"history",
    routes:[
        {
            path:"/",
            redirect:"/home"
        },
        {
            path:"/home",
            component: resolve => require(["@/views/Home"], resolve)
        },
        {
            path:"/Reg",
            component:()=> import ("../views/Reg.vue")
        },
        {
            name: "/GoodsDetail",
            path:"/GoodsDetail/:goodsid",
            component:GoodsDetail
        },
        {
            path:"/My",
            component:My,
        },
        {
            path:"/Cart",
            component:Cart,
            /* beforeEnter(to, from, next){
                if(!localStorage.getItem("token")){
                    next("/Login?path="+to.path);
                }
                else {
                    next();
                }
            } */
            meta:{
                requiresAuth:true
            }
        },
        {
            path:"/Sort",
            component:Sort
        },
        {
            path:"/Login",
            component:Login
        },
        {
            path: "/Search",
            component: Search
        },
        {
            path: "/Setting",
            component: Setting
        },
        {
            path: "/SearchResult",
            component: SearchResult
        },
        {
            path: "/payMent",
            component: payMent
        }
    ]
});
 /* 全局前置  */
router.beforeEach((to, from, next)=>{
    console.log("全局前置钩子");
    if (to.meta.requiresAuth) {
        if (!localStorage.getItem("token")) {
            next("/Login?path="+to.path);
        }
    }
    next()
})
/*  全局后置 */
router.afterEach((to,from)=>{
    console.log("全局后置钩子！")
    // next();
})
export default router;